package com.springboot.frame.starter.web.captcha;


import com.springboot.frame.web.captcha.Captcha;
import com.springboot.frame.web.captcha.CaptchaConfig;
import com.springboot.frame.web.captcha.DefCaptcha;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 验证码 的spring-boot启动器
 *
 * @author TaoYu MaYing
 */
@Configuration
@EnableConfigurationProperties(CaptchaProperties.class)
@ConditionalOnProperty(name = "web.captcha.enabled")
public class CaptchaAutoConfiguration {

    private final CaptchaProperties properties;

    public CaptchaAutoConfiguration(CaptchaProperties properties) {
        this.properties = properties;
    }

    @Bean
    // 如果已存在实现则不再注入此bean
    @ConditionalOnMissingBean
    public Captcha localCaptcha() {
        CaptchaConfig captchaConfig = new CaptchaConfig();
        BeanUtils.copyProperties(properties, captchaConfig);
        // 默认实现，也可自定义实现
        return new DefCaptcha(captchaConfig);
    }

}
